package com.dbq.fast.business.order.api;

import android.util.Log;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.login.api.LoginApi;
import com.dbq.fast.business.login.bean.CustomProfile;
import com.dbq.fast.business.order.bean.OrderInfo;
import com.dbq.fast.business.product.api.CommonTransformer;
import com.dbq.fast.common.json.ParamsTool;
import com.dbq.fast.common.manager.DatabaseManager;
import com.dbq.fast.common.net.HttpManager;
import com.dbq.fast.common.net.RemoteApiCallBack;
import com.dbq.fast.util.SystemUtil;

import java.util.HashMap;

import okhttp3.RequestBody;


public class OrderApi {
    private static HttpManager httpManager = HttpManager.getInstance();

    //获取最近十个订单
    public static void getOrderList(CustomProfile profile, RemoteApiCallBack<OrderInfo> c) {
        if(profile == null || profile.getAccount() == null) return;
        HashMap hashMap = new HashMap();
        hashMap.put("account", profile.getAccount());
        String bodyStr = ParamsTool.hashMapToJson(hashMap);
        RequestBody body = httpManager.getPostBody(bodyStr);
        httpManager.getHttpService()
                .getOrderList(SystemUtil.getLanguageCustom(App.getContext()), profile.getToken(), body)
                .compose(new CommonTransformer<>())
                .subscribe(result -> {
                    Log.e("vpn_log", "result:" + result.getData().toString());
                    int code = result.getCode();
                    if (code == 200 || code == 426) {
                        OrderInfo orderInfos = result.getData();
                        c.onSuccess(orderInfos);
                    } else if (code == 401) { //token失效
                        LoginApi.refreshToken(true, new RemoteApiCallBack<Boolean>() {
                            @Override
                            public void onSuccess(Boolean aBoolean) {
                                CustomProfile profileNew = DatabaseManager.getInstance().queryProfile();
                                getOrderList(profileNew, new RemoteApiCallBack<OrderInfo>() {
                                    @Override
                                    public void onSuccess(OrderInfo orderInfo) {
                                        OrderInfo orderInfos = result.getData();
                                        c.onSuccess(orderInfos);
                                    }

                                    @Override
                                    public void onFail(int status, String message) {
                                        c.onFail(code, App.getContext().getString(R.string.tip_retry_network_bad));
                                    }
                                });
                            }

                            @Override
                            public void onFail(int status, String message) {
                                c.onFail(code, App.getContext().getString(R.string.tip_retry_network_bad));
                            }
                        });
                    } else if (code == 402) { //其他设备登录
                        c.onFail(code, ""/*App.getContext().getString(R.string.login_on_other_device)*/);
                    } else {
                        c.onFail(code, App.getContext().getString(R.string.tip_retry_network_bad));
                    }
                }, throwable -> {
                    c.onFail(-1, App.getContext().getString(R.string.tip_retry_network_error));
                });
    }

}
